<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" :model = "form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input placeholder = "请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder = "请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="form.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="login">登 陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reqLogin } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  // 方便开发者维护
  name: 'Login',
  data () {
    var isAgree = (rule, value, callback) => {
      if (value) {
        callback()
      } else {
        callback(new Error('请勾选选框'))
      }
    }
    return {
      form: {
        mobile: '13911111111',
        code: '246810',
        agree: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '请输入合法的手机号', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: ['blur', 'change'] },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码', trigger: ['blur', 'change'] }
        ],
        agree: [
          { validator: isAgree, trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async login () {
      try {
        await this.$refs.form.validate()
      } catch {
        return console.log('验证失败')
      }
      // 表单验证通过发送axios请求
      try {
        const res = await reqLogin(this.form.mobile, this.form.code)
        if (res.status === 201) {
          this.$message({
            message: '登录成功',
            type: 'success',
            duration: 1000
          })
          // 存token
          setToken(res.data.data.token)
          this.$router.push('/')
        } else {
          this.$message.warning('服务器异常!')
        }
      } catch {
        this.$message.error('手机号或验证码错误!')
      }
    }
  }
}
</script>

<style lang="less">

.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("~@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    width: 400px;
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    box-shadow: 5px 10px 15px #333;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("~@/assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
     .login-btn {
        width: 100%;
        }
      }
  }
}
</style>
